/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月7日         luwei
 */

package com.coocaa.app.gamecenter.pages.home;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.coocaa.app.gamecenter.framework.ui.MultiImageView;
import com.coocaa.launcher.framework.sidebar.item.ItemView;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.framework.utils.NativeImageCache.SmartBitmap;
import com.coocaa.x.uipackage.utils.CoocaaFresco;
import com.coocaa.x.uipackage.widget.CoocaaDraweeView;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.widget.AlwaysMarqueeTextView;

public class RecommendItemView extends ItemView {
    private TextView mName = null;
    private CoocaaDraweeView mIcon = null;
    private ImageView mLabel = null;
    private MultiImageView mControlIcon = null;

    public RecommendItemView(Context context) {
        super(context);
        addIcon();
        addNameView();
        addControlIcon();
    }

    private void addIcon() {
        mIcon = new CoocaaDraweeView(mContext);
        mIcon.setBackgroundResource(R.drawable.game_center_item_bg_shadow);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        params.bottomMargin = Div(64);
        mIcon.setLayoutParams(params);
        mIcon.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY);
        mIcon.getHierarchy().setPlaceholderImage(R.drawable.game_center_default);
        mIcon.setPadding(Div(14), Div(24), Div(14), Div(22));
        addView(mIcon);
    }

    private void addNameView() {
        mName = new AlwaysMarqueeTextView(mContext);
        mName.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
        Drawable draw = getResources().getDrawable(R.drawable.pic_nopic);
        mName.setCompoundDrawables(draw, null, null, draw);
        mName.setCompoundDrawablePadding(10);
        mName.setTextSize(Dpi(33));

        mName.setTextColor(Color.WHITE);
        mName.setPadding(Div(22), 0, Div(53), Div(8));
        LayoutParams params = new LayoutParams(Div(330), Div(90), Gravity.BOTTOM);
        params.leftMargin = Div(6);
        addView(mName, params);
        mName.setBackgroundResource(R.drawable.game_center_item_title_bg);
    }

    private void addLabel() {
        mLabel = new ImageView(mContext);
        LayoutParams params = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.RIGHT);
        mLabel.setLayoutParams(params);
        addView(mLabel);
    }

    private void addControlIcon() {
        mControlIcon = new MultiImageView(mContext);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, Div(50), Gravity.BOTTOM);
        mControlIcon.setDivide(Div(10));
        params.rightMargin = Div(45);
        params.bottomMargin = Div(24);
        addView(mControlIcon, params);
    }

    public void updateControlIcon(SmartBitmap[] bitmaps) {
        mControlIcon.setImages(bitmaps);
    }

    @Override
    public void destory() {
        Drawable drawable = mIcon.getDrawable();
        if (drawable != null) {
            drawable.setCallback(null);
        }
        mIcon.setBackgroundDrawable(null);
        mControlIcon.destory();
    }

    public void updateName(final String name) {
        post(new Runnable() {
            @Override
            public void run() {
                mName.setText(name);
            }
        });
    }

    public void updateIcon(final Bitmap bitmap) {
        LogUtils.d("luwei", "the width " + bitmap.getWidth());
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mIcon.setImageBitmap(bitmap);
            }
        });

    }


    public void updateIcon(String url, int width, int height) {
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                .setResizeOptions(new ResizeOptions(width, height))
                .build();
        DraweeController controller = CoocaaFresco.newDraweeControllerBuilder(mContext)
                .setOldController(mIcon.getController())
                .setImageRequest(request)
                .build();
        mIcon.setController(controller);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            mName.setSelected(true);
        } else {
            mName.setSelected(false);
        }
        super.onFocusChange(v, hasFocus);
    }
}
